<?php

namespace App\Http\Controllers\Api;

use App\Models\Shop;
use App\Transformers\BannerTransformer;
use App\Transformers\ShopTransformer;
use Illuminate\Http\Request;

class ShopsController extends Controller
{
    public function index(Request $request)
    {
        $name = $request->get('name');

        $where = $request->only([
            'province', 'city', 'district', 'recommended',
        ]);

        $limit = $request->get('limit');

        $query = Shop::where('status', Shop::PASS)
            ->where(function ($query) use ($name) {
                if ($name) {
                    $query->where('name', 'like', '%' . $name . '%');
                }
            })
            ->where($where)->orderBy('recommended', 'desc')->orderBy('sort', 'desc');

        if ($limit) {
            $data = $query->limit($limit)->get();

            return $this->response->collection($data, new ShopTransformer(['id', 'name', 'face_picture']));
        } else {
            $data = $query->paginate();

            return $this->response->paginator($data, new ShopTransformer(['id', 'name', 'face_picture']));
        }

    }

    public function show($id)
    {
        $data = Shop::find($id);

        if (!$data) {
            $this->response->error(trans('api.not_exists'), 200);
        }

        return $this->response->item($data, new ShopTransformer());
    }

}
